package com.redpacketcover.rc.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.redpacketcover.rc.Constants;
import com.redpacketcover.rc.R;
import com.redpacketcover.rc.bean.GoodsRecord;
import com.redpacketcover.rc.goods.GoodsDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class GoodsListAdapter extends BaseAdapter {

    private Context context;
    private List<GoodsRecord> list;
    private LayoutInflater layoutInflater;

    List<String> taglist;

    public GoodsListAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list != null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_goods_list, null);
            holder = new ViewHolder();
//            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.goods_img = convertView.findViewById(R.id.goods_img);
            holder.goods_title = convertView.findViewById(R.id.goods_title);
            holder.goods_price = convertView.findViewById(R.id.goods_price);
            holder.goods_oldprice = convertView.findViewById(R.id.goods_oldprice);
            holder.btn_layout = convertView.findViewById(R.id.btn_layout);
            holder.goods_tag_layout = convertView.findViewById(R.id.goods_tag_layout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GoodsRecord provinceBean = list.get(position);
        if (provinceBean != null) {
//            holder.text.setText(provinceBean.getName());
//            holder.text.setBackgroundResource(provinceBean.getColor());
            holder.goods_title.setText(provinceBean.getTitle());
            holder.goods_price.setText("¥"+provinceBean.getPrice());
            holder.goods_oldprice.setText("¥"+provinceBean.getOldPrice());
            holder.goods_tag_layout.removeAllViews();
            taglist = new ArrayList<>();
            if(provinceBean.getTagArr()!=null){
                for(int i=0 ;i<provinceBean.getTagArr().size();i++){
                    taglist.add(provinceBean.getTagArr().get(i).getTitle());
                    View view = initTagView();
                    if(view != null){
                        holder.goods_tag_layout.addView(view);
                    }
                }
            }
            Glide.with(context).load(Constants.URL_HOST_TEST+provinceBean.getImg())
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(8))
                            .placeholder(R.drawable.bg_corner_10_e5e5e5_shape)
                            .override(76,76)).into(holder.goods_img);
            holder.btn_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, GoodsDetailActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    intent.putExtra("id",provinceBean.getId());
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    private View initTagView() {
        if(taglist.size() == 0){
            return null;
        }else{
            View view = View.inflate(context, R.layout.tag_layout, null);
            for(int i = 0;i<taglist.size();i++){
                TextView tv = (TextView) view.findViewById(R.id.tag_title);
                tv.setText(taglist.get(i));
            }
            return view;
        }
    }

    class ViewHolder {
        LinearLayout btn_layout;
        ImageView goods_img;
        TextView goods_title;
        TextView goods_price;
        TextView goods_oldprice;
        LinearLayout goods_tag_layout;
    }

    public void clear() {
        if(this.list!= null){
            this.list.clear();
        }
    }

    public void setData(List<GoodsRecord> mList) {
        list = mList;
    }
}
